<template>
    <el-dialog
            title="添加楼栋"
            :visible.sync="dialogVisible"
            width="30%"
            :close-on-click-modal = false>
        <el-form ref="editform" :rules="rules" :model="editUser" label-width="80px">
            <el-form-item label="楼栋编号" prop="id">
                <el-input v-model="editUser.buildingId" placeholder="请输入楼栋编号"></el-input>
            </el-form-item>
            <el-form-item label="楼层" prop="floor">
                <el-input v-model="editUser.totalFloor" placeholder="请输入楼层"></el-input>
            </el-form-item>
            <el-form-item label=面向对象 prop="type">
                <el-select v-model="editUser.type" style="width: 100%" placeholder="请选择">
                    <el-option
                            v-for="item in roles"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="面向性别" prop="gender">
                <el-select v-model="editUser.sex" style="width: 100%" placeholder="请选择">
                    <el-option
                            v-for="item in gender"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="联系方式">
                <el-input v-model="editUser.contactWay" placeholder="请输入联系方式"></el-input>
            </el-form-item>
            <el-form-item label="物业单位信息">
                <el-input v-model="editUser.propertyUnit" placeholder="请输入物业单位相关信息"></el-input>
            </el-form-item>
            <el-form-item label="楼管姓名">
                <el-input v-model="editUser.residentialManager" placeholder="请输入楼管姓名"></el-input>
            </el-form-item>
            <el-form-item label="住楼辅导员信息">
                <el-input v-model="editUser.teacherName" placeholder="请输入姓名"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import {editUser} from '../../../../api/third'

export default {
  name: 'UserImportDialog',
  data: function () {
    return {
      dialogVisible: false,
      editUser: {},
      roles: [],
      gender: [{
        value: '男',
        label: '男'
      }, {
        value: '女',
        label: '女'
      }, {
        value: '不限',
        label: '不限'
      }],
      rules: {
        buildingId: [
          { required: true, message: '请输入楼栋编号', trigger: 'blur' }
        ],
        totalFloor: [
          { required: true, message: '请输入楼层', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
        type: [
          {required: true, message: '请选择面向对象', trigger: 'blur'}
        ]
      },
      query: {}
    }
  },
  methods: {
    show (user, roles) {
      // user和传过来的roles是同一个地址，修改会影响
      // 一般会进行克隆
      // 将对象先json字符串化，再json对象化
      this.editUser = JSON.parse(JSON.stringify(user))
      // editUser改头换面，先字符串化，再对象化，成为全新的变量
      this.roles = roles
      this.imageUrl = this.editUser.url
      this.dialogVisible = true
    },
    submit () {
      this.$refs.editform.validate(valid => {
        if (valid) {
          // 提交数据的逻辑
          editUser(this.editUser).then(res => {
            if (res.data === true) {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.dialogVisible = false // 关闭对话框
              this.$emit('ok') // 向父组件发送消息
            }
          })
        } else {
          // 如果表单验证不通过，可以给出提示信息
          this.$message.error('请填写正确的表单信息')
        }
      })
    },
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.editUser.url = res.url
    },
    beforeAvatarUpload (file) {
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}

/deep/ .el-upload{
    border: 1px dotted #000000;
}

</style>
